//
//  WmcSyncSettingsVM.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/9/17.
//

import Foundation
import YPNetwork

class WmcSyncSettingsVM: WmcBaseVM {
    
    private(set) var dataSource: [WmcMediaAlbumConfigurationModel] = []
    
    func fetchTeamList(completion: @escaping ([WmcMediaAlbumConfigurationModel]) -> Void, failure: @escaping (Error?) -> Void) {
        
        WmcMediaTeamManagerAPI.fetchConfiguration().subscribe(onNext: { [weak self] list in
            guard let weakSelf = self else { return }
            weakSelf.dataSource = list
            completion(list)
        }, onError: { error in
            failure(error)
        }).disposed(by: rx.disposeBag)
    }
    
    func updateSwitch(model: WmcMediaAlbumConfigurationModel, isOn: Bool, completion: @escaping (YPNetworkResponse) -> Void, failure: @escaping (Error?) -> Void) {
        var teamId: Int? = nil
        if model.albumType == 1 {
            teamId = model.busId
        }
        WmcMediaTeamManagerAPI.updateConfiguration(albumId: model.albumId, isOpen: isOn, teamId: teamId).subscribe(onNext: { resp in
            completion(resp)
        }, onError: { error in
            failure(error)
        }).disposed(by: rx.disposeBag)
    }
    
    func updateStatus(model: WmcMediaAlbumConfigurationModel, isOn: Bool) {
        var temp: [WmcMediaAlbumConfigurationModel] = []
        dataSource.forEach { item in
            if item.albumId == model.albumId {
                item.isSync = isOn
            }
            temp.append(item)
        }
        dataSource = temp
    }
    
    func querySelectedCount() -> Int {
        var count: Int = 0
        
        dataSource.forEach { model in
            if model.isSync {
                count += 1
            }
        }
        
        return count
    }
    
    func querySelectdTeamCount() -> Int {
        var count: Int = 0
        
        dataSource.forEach { model in
            if model.isSync {
                count += 1
            }
        }
        
        return count
    }
}
